
#include "headfile.h"
#include "key.h"
//按键初始化函数
key_para key_msg;
#define ls(a)	lcd_showint16(75,key_msg.hang,a)


float add_k;
float cali_sx,cali_sy;
#define ls_var(a,b)		lcd_showint16(75,a,b)
int flag_delay=0;
void ui_showstr()
{
	lcd_showstr(20,1,"mode");
	lcd_showstr(20,2,"kp");
	lcd_showstr(20,3,"ki");
	lcd_showstr(20,4,"steerx");
	lcd_showstr(20,5,"steery");
	lcd_showstr(20,6,"tx");
	lcd_showstr(20,7,"ty");
	lcd_showstr(20,8,"mode");
}
void show_hang_point(void)
{
	lcd_showstr(0,key_msg.hang,"--");
	if (key_msg.hang>0)lcd_showstr(0,key_msg.hang-1,"  ");
	lcd_showstr(0,key_msg.hang+1,"  ");

}

void 	ui_show_var()
{
	//	lcd_showint16(75,2,12);
//		lcd_showint16(75,3,12);
		//lcd_showint16(75,4,offset_x_test);
		//lcd_showint16(75,5,12);
		ls_var(4,steer_x.output);
		ls_var(5,steer_y.output);
	//	ls_var(6,12);
		//ls_var(7,12);
		//ls_var(8,12);
}
void 	ui_show_avar()
{
		ls_var(2,kp);
		ls_var(3,ki*100);
		ls_var(4,steer_x.output);
		ls_var(5,steer_y.output);
//		lcd_showint16(75,4,offset_x_test);
//		lcd_showint16(75,5,offset_y_test);
		ls_var(6,offset_x_test);
		ls_var(7,offset_y_test);
		ls_var(8,12);
}

void ui_main2(void)
{
	Key_Type_e flag_key=key_scan();
	switch (flag_key){
		case Number4:
			switch (key_msg.hang){
				case 1:
					mode-=1;
					update_mode_show();
					//steer_info_init();
					break;
				case 2:
					kp-=2;

					break;				
				case 3:
					ki-=1;
					break;
				case 4:
					role1_map_msg.set_x-=430;
					offset_x_test+=200;
					break;
				case 5:
					role1_map_msg.set_y-=330;
					offset_y_test-=400;
					break;
				case 6:
					offset_x_test-=100;
				
					break;
				case 7:
					offset_y_test-=5;
					break;
				case 8:
					break;				
			}
			break;
		case Number6:
			switch (key_msg.hang){
				case 1:
					mode++;
					update_mode_show();
					break;
				case 2:
					kp+=2;
					break;				
				case 3:
					ki+=1;
					break;
				case 4:
					role1_map_msg.set_x+=460;
					offset_x_test+=400;

					break;
				case 5:
					role1_map_msg.set_y+=360;
					offset_y_test+=40;

					break;
				case 6:
					offset_x_test+=100;
					break;
				case 7:
					offset_y_test+=100;
					break;
				case 8:
					break;				
			}
			break;
		
		case Number1:
			key_msg.page++;
			break;
		case Number2:
			key_msg.hang--;
			show_hang_point();
			break;
		case Number3:
			key_msg.page--;
			break;

		case Number5:
			send_mode();
			break;
		case Number7:
			send_stop();
			break;
		case Number8:
			key_msg.hang++;
			show_hang_point();
			break;			
		case Number9:
			
			break;	
	}
	static int flag_temp;
	flag_temp++;
	if(flag_temp%10==0)
	{
		ui_show_var();
	}
	if(flag_key)	
		ui_show_avar();

}

static void update_mode_show(void)
{
	lcd_showstr(70,1,"     ");

	if(mode==0)
		lcd_showstr(70,1,"reset");
	else if (mode==1)
		lcd_showstr(70,1,"white");
	else if (mode==-1)
		lcd_showstr(70,1,"cali");
	else if (mode==-2)
		lcd_showstr(70,1,"find_red");
	else if (mode==2)
		lcd_showstr(70,1,"black");
	else if (mode==3)
		lcd_showstr(70,1,"blcak2");
}





void KEY_Init(void) //IO初始化
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);  //使能PE,PC时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);  
	GPIO_InitStructure.GPIO_Pin  =GPIO_Pin_13;     
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Not used in input mode

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;    
	
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;      
	GPIO_Init(GPIOC, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Pin  =GPIO_Pin_5|GPIO_Pin_6;             
	GPIO_Init(GPIOE, &GPIO_InitStructure);	

	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			 //IO口速度为50MHz
	GPIO_Init(GPIOE, &GPIO_InitStructure);		
}

 int read_key_input(void)
{
    if(KEY_SCAN4==1)return 1;
    if(KEY_SCAN5==1)return 2;
    if(KEY_SCAN6==1)return 3;
    return 0;
}
 Key_Type_e read_matrix_key(void)
{
    int i=0;
    KEY_SET1;
    KEY_RESET2;
    KEY_RESET3;

    i=read_key_input();
    switch(i)
    {
        case 1: return Number1;
        case 2: return Number2;
        case 3: return Number3;
        default:break;
    }
    KEY_RESET1;
    KEY_SET2;
    i=read_key_input();
    switch(i)
    {
        case 1: return Number4;
        case 2: return Number5;
        case 3: return Number6;
        default:break;
    }
		KEY_RESET2;
		KEY_SET3;
    i=read_key_input();
    switch(i)
    {
        case 1: return Number7;
        case 2: return Number8;
        case 3: return Number9;
        default:break;
    }
		KEY_RESET1;
		KEY_RESET2;
		KEY_RESET3;
		return Key_None;
}
Key_Type_e key_scan(void){
    static Key_Type_e key_last;
    Key_Type_e key_temp=read_matrix_key();
    if(key_temp!=0&&key_temp!=key_last){
        key_last=key_temp;
        return key_temp;
    }
    else         
			key_last=key_temp;
		return Key_None;
}

